/**
 * Created by Administrator on 2016/6/7.
 */
//切换登录注册窗口
$("document").ready(function(){
    $("nav section section").click(function(){
        $("nav section section").removeClass().addClass("unFocus");
        $(this).addClass("focus");
    });
    $("nav section section:first-child").click(function(){
        $("section.rForm").css("display","block");
        $("section.lForm").css("display","none");
    });
    $("nav section section:last-child").click(function(){
        $("section.lForm").css("display","block");
        $("section.rForm").css("display","none");
    })
})
//登录注册窗口细节动画
//注册手机号
$("document").ready(function(){
    var $rCeilPhone = $("input.ceilPhone");
    var img = $rCeilPhone.next("img");
        $rCeilPhone.focus(function(){
            img.attr("src","../images/delete_2.png");
        }).keyup(function(){
            img.attr("src","../images/delete_2.png");
        }).blur(function(){
            img.attr("src","../images/delete_1.png");
            //手机号验证通过获取验证码可点击
            var ceilER =$("#ceilPhone-error");
            if(ceilER.hasClass("success")){
                $("#securityCode").removeAttr("disabled");
            }
        });
    img.click(function(){
        $rCeilPhone.val("");
    })

})
//登录密码可视切换
$("document").ready(function(){
    var $rPassword = $("input.password");
    var img = $rPassword.next("img");
    var i = 1;
    img.click(function(){
       i = i + 1;
        if(i%2==0){
            img.attr("src","../images/visible_2.png");
            $rPassword.attr("type","text");
        } else{
            img.attr("src","../images/visible_1.png");
            $rPassword.attr("type","password");
        }
    })
})
//服务条款
$("document").ready(function(){
    $("#rForm p a").click(function(){
        location.href="serviceRule.html";
    })
})
//忘记密码跳转
$("document").ready(function(){
  $("#lForm a").click(function(){
      location.href="forgetPassword.html";
  })
})

//60秒重新发送
var countdown=60;
function settime(obj) {
    if (countdown == 0) {
        obj.removeAttribute("disabled");
        obj.innerHTML="获取验证码";
        return;
    } else {
        obj.setAttribute("disabled", true);
        obj.innerHTML="重新发送(" + countdown + ")";
        countdown--;
    }
    setTimeout(function() {
            settime(obj) }
        ,1000)
}

//表格验证
//添加手机号验证规则
$("document").ready(function(){
    //获得焦点错误信息删除
    $("#rForm input").focus(function(){
            var $name=$(this).attr("name");
            var emID ="#"+$name+"-error";
            $(emID).css("display","none");
        })
        //全部验证通过注册按钮解锁
    $("#rForm input").blur(function(){
                var emSuccess = $("em.success");
                if(emSuccess.length==4){
                    $(".register").removeAttr("disabled");
                }
            })

    //添加手机验证规则
    $.validator.addMethod("isMobile", function(value, element) {
    var length = value.length;
    var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
    return this.optional(element) || (length == 11 && mobile.test(value));
}, "请正确填写您的手机号码");

    ////注册表单验证规则
    //$("#rForm").validate({
    //    onkeyup:false,
    //    rules : {
    //        ceilPhone:{
    //            required : true,
    //            isMobile : true,
    //            number:true
    //        },
    //        securityCode1:{
    //            number:true,
    //            required : true
    //        },
    //        password:{
    //            required : true
    //        },
    //        username : {
    //            required : true
    //        }
    //    },
    //    messages : {
    //        ceilPhone : {
    //            required : "手机号不能为空",
    //            isMobile : "手机号不合法",
    //            number:"手机号不合法"
    //        },
    //        securityCode1:{
    //            required : "验证码不能为空",
    //            number:"验证码错误",
    //        },
    //        password:{
    //            required : "密码不能为空"
    //        },
    //        username : {
    //            required : "昵称不能为空",
    //        }
    //    },
    //    errorElement: "em",
    //    success: function(lable) {
    //        lable.text(" ")
    //            .addClass("success");
    //    },
    //    //em元素放置位置
    //    errorPlacement: function(error, element) {
    //        error.appendTo( element.parent().parent() );
    //    }
    //});
})

//Ajax验证码接口请求
$("document").ready(function(){
    $("#securityCode").click(function(){
        var value = $(".ceilPhone").val();
        $.post(commonval+"/sendTelMsValCode",{
            type:"api_register",
            tel:value,
            device:"wap"
        },function(data){
            var status = data.status;
            if(status == 0){
                createWindow("发送失败");
            }else if(status == 1){
                createWindow("发送成功");
            }
        })
    })
})

//ajax登录接口请求
$("document").ready(function(){
    $(".lregister").click(function(){
        var str = window.location.search;
        var jump = str.replace(/[^0-9]+/g, '')
        var ceilPhone = $(".lceilPhone").val();
        var password = $(".lpassword").val();
        $.post(commonval+"/login",{
            username:ceilPhone,
            password:password,
            device:"wap"
        },function(data){
            console.log(data.data);
            var status = data.status;
            if(status == 0){
                createWindow(data.msg);
                function createWindow(msg) {
                    var window =$("<section class='showMsg'></section>");
                    var show = "<p>"+msg+"</p>";
                    window.append(show);
                    $("body").append(window);
                    setTimeout("$('.showMsg').remove()",3000);
                }
            }else if(status == 1){
                createWindow("登录成功,跳转回登录前界面");
                sessionStorage.u_id = data.data.u_id;
                sessionStorage.nic_thumb =  data.data.nic_thumb;
                sessionStorage.username = data.data.username;
                sessionStorage.nic_name = data.data.nic_name;
                sessionStorage.user_token = data.data.user_token;
                sessionStorage.session_id = data.data.session_id;
                console.log(sessionStorage.session_id);
                if(data.data.nic_thumb ===null||data.data.nic_thumb==''){
                    sessionStorage.nic_thumb = '';
                }else{
                    sessionStorage.nic_thumb =  data.data.nic_thumb;
                }
                if(jump == 2){
                    location.href = 'consult.html';
                }else if(jump == 1){
                    location.href = 'doctorInfo.html';
                }else if(jump == 3){
                    location.replace('mine.html');
                }
                else if(jump == 4){
                    location.replace('doctorJoin.html');
                }
                else if(jump == 5){
                    location.replace('mine.html');
                } else if(jump == 6){
                    location.href='diaryBook.html';
                }
                else if(jump == 7){
                    location.href='submitOrder.html';
                }else if(jump == 8){
                    location.href='toutiaoWeb.html';
                }
                function createWindow(msg) {
                    var window =$("<section class='showMsg'></section>");
                    var show = "<p>"+msg+"</p>";
                    window.append(show);
                    $("body").append(window);
                    setTimeout("$('.showMsg').remove()",3000);
                }
            }
        })
    })
})


//Ajax 注册接口请求
$("document").ready(function(){
    $(".register").click(function(){
        var ceilPhone = $(".ceilPhone").val();
        var Security = $(".rSecurityCode").val();
        var password = $(".password").val();
        var nickName = $(".rUsername").val();
        $.post(commonval+"/register",{
            username:ceilPhone,
            password:password,
            nickname:nickName,
            code:Security,
            device:"wap"
        },function(data){
            var status = data.status;
            if(status == 0){
                createWindow(data.msg);
                function createWindow(msg) {
                    var window =$("<section class='showMsg'></section>");
                    var show = "<p>"+msg+"</p>";
                    window.append(show);
                    $("body").append(window);
                    setTimeout("$('.showMsg').remove()",3000);
                }
            }else if(status == 1){
                createWindow("注册成功，跳转回登录前界面");
                function createWindow(msg) {
                    var window =$("<section class='showMsg'></section>");
                    var show = "<p>"+msg+"</p>";
                    window.append(show);
                    $("body").append(window);
                    setTimeout("$('.showMsg').remove()",3000);
                }
                var str = window.location.search;
                var jump = str.replace(/[^0-9]+/g, '')
                var ceilPhone = $(".ceilPhone").val();
                var password = $(".password").val();
                $.post(commonval+"/login",{
                    username:ceilPhone,
                    password:password,
                    device:"wap"
                },function(data){
                    console.log(data);
                    var status = data.status;
                    if(status == 0){
                    }else if(status == 1){
                        sessionStorage.u_id = data.data.u_id;
                        sessionStorage.nic_thumb =  data.data.nic_thumb;
                        sessionStorage.username = data.data.username;
                        sessionStorage.nic_name = data.data.nic_name;
                        sessionStorage.user_token = data.data.user_token;
                        sessionStorage.session_id = data.data.session_id;

                        if(data.data.nic_thumb ===null||data.data.nic_thumb==''){
                            sessionStorage.nic_thumb = '';
                        }else{
                            sessionStorage.nic_thumb =  data.data.nic_thumb;
                        }
                        if(jump == 2){
                            location.href = 'consult.html';
                        }else if(jump == 1){
                            location.href = 'doctorInfo.html';
                        }else if(jump == 3){
                            location.replace('mine.html');
                        }
                        else if(jump == 4){
                            location.replace('doctorJoin.html');
                        }else if(jump == 5){
                            location.replace('mine.html');
                        }
                        else if(jump == 6){
                            location.href='diaryBook.html';
                        }else if(jump == 7){
                            location.href='submitOrder.html';
                        }else if(jump == 8){
                            location.href='toutiaoWeb.html';
                        }
                    }
                })
                $("nav section a:last-child").click().blur();
            }
        })
    })
})